package com.example.controller;

import com.example.service.OrderDetailsService;
import com.example.common.Result;
import com.example.entity.OrderDetails;
import com.example.entity.Params;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/order-details")
public class OrderDetailsController {

    private static final Logger log = LoggerFactory.getLogger(OrderDetailsController.class);

    @Resource
    private OrderDetailsService orderDetailsService;

    @GetMapping
    public Result findAll() {
        List<OrderDetails> list = orderDetailsService.findAll();
        return Result.success(list);
    }

    @GetMapping("/search")
    public Result findBySearch(Params params) {
        log.info("调用接口查询订单详情信息");
        PageInfo<OrderDetails> info = orderDetailsService.findBySearch(params);
        return Result.success(info);
    }

    @PostMapping
    public Result save(@RequestBody OrderDetails detail) {
        if (detail.getDetailId() == null) {
            orderDetailsService.add(detail);
        } else {
            orderDetailsService.update(detail);
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer detailId) {
        orderDetailsService.delete(detailId);
        return Result.success();
    }
}
